//
//  LZDateExtension.swift
//  halikaPeso
//
//  Created by L-zh on 2024/12/16.
//

import UIKit

extension Date {
//    获取当前时间
    static func getCurrentDate(date:Date?) ->String? {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = "YYYY-MM-dd"
        if let currentDate = date {
         let currentDateStr = dateformatter.string(from: currentDate)
            return currentDateStr
        }
        
        return nil
    }
    //获取六个月后
    static func getAfterSixMonthsDate() ->String? {
        
        let currentDate = Date()
        let calendar = NSCalendar(calendarIdentifier: .republicOfChina)
        let components = NSCalendar.Unit()
        var comps = calendar?.components(components, from: currentDate)
        comps?.year = 0
        comps?.month = -6
        comps?.day = 0
        if let  newComps = comps {
            let newDate = calendar?.date(byAdding: newComps, to: currentDate, options: NSCalendar.Options.init())
            return getCurrentDate(date: newDate)
        }
        return nil
        
    }
    //获取两年后
    static func getAfterTwoYearDate() ->String? {
        
        let currentDate = Date()
        let calendar = NSCalendar(calendarIdentifier: .republicOfChina)
        let components = NSCalendar.Unit()
        var comps = calendar?.components(components, from: currentDate)
        comps?.year = -2
        comps?.month = 0
        comps?.day = 0
        if let  newComps = comps {
            let newDate = calendar?.date(byAdding: newComps, to: currentDate, options: NSCalendar.Options.init())
            return getCurrentDate(date: newDate)
        }
        return nil
        
    }
    //获取10年后
    static func getAfterTenYearDate() ->String? {
        
        let currentDate = Date()
        let calendar = NSCalendar(calendarIdentifier: .republicOfChina)
        let components = NSCalendar.Unit()
        var comps = calendar?.components(components, from: currentDate)
        comps?.year = -10
        comps?.month = 0
        comps?.day = 0
        if let  newComps = comps {
            let newDate = calendar?.date(byAdding: newComps, to: currentDate, options: NSCalendar.Options.init())
            return getCurrentDate(date: newDate)
        }
        return nil
        
    }
    
    /// 14位时间戳转字符串
    ///
    /// - Parameter timeStamp: 时间戳
    /// - Returns: 时间字符串
    static func  timeStampToStr(timeStamp:Int) -> String? {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = "YYYYMMddHHmmss" //自定义日期格式
      
         let tempDate = dateformatter.date(from: String(timeStamp))
//        let timeInterval:TimeInterval = TimeInterval(timeStamp)
//        let timeStampDate = NSDate(timeIntervalSince1970: timeInterval * 1000 )
        dateformatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        
      let time = dateformatter.string(from: tempDate ?? Date())
        return time
    }
    
    
    
}

